package com.momo.mal.manga.overall;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import net.momo.mal.R;
import com.momo.mal.util.ConnectionUtil;
import com.momo.mal.util.DialogSearch;
import com.momo.mal.util.slidingmenu.BaseActivity;
import com.slidingmenu.lib.SlidingMenu;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;

public class MangaOverall extends SherlockActivity {

	private MangaOverallAdapter adapter;
	private ViewPager pager;
	private PageIndicator indicator;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.anime_overall);
		setTitle("My Manga List");

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		String[] params = new String[0];
		if (getIntent().getStringExtra("username") != null) {
			params = new String[] { getIntent().getStringExtra("username") };
			setTitle(params[0] + "'s Manga List");
		} else {
			setTitle("My Manga List");
		}

		new RequestMangaList(this).execute(params);

	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		menu.add(getResources().getString(R.string.menu_search))
//				.setIcon(R.drawable.ic_menu_search)
//				.setOnMenuItemClickListener(new OnMenuItemClickListener() {
//
//					@Override
//					public boolean onMenuItemClick(MenuItem item) {
//						new DialogSearch().newInstance("manga").show(
//								getSupportFragmentManager(), null);
//						return false;
//					}
//				})
//				.setShowAsAction(
//						MenuItem.SHOW_AS_ACTION_IF_ROOM
//								| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
//
//		return true;
//	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private class RequestMangaList extends AsyncTask<String, Void, JSONObject> {
		private ProgressDialog dialog;
		private Context context;

		public RequestMangaList(Context cxt) {
			dialog = new ProgressDialog(cxt);
			dialog.setCancelable(false);
			this.context = cxt;
		}

		@Override
		protected void onPreExecute() {
			dialog.setMessage("Loading Data ...");
			dialog.show();
		}

		@Override
		protected JSONObject doInBackground(String... params) {
			JSONObject json = new JSONObject();
			try {
				if (params.length > 0) {
					json = ConnectionUtil.get(ConnectionUtil.SECONDATY_API_HOST
							+ "mangalist/" + params[0], context);
				} else {
					json.put("response", "success");
				}
			} catch (JSONException e) {
				Toast.makeText(context,
						getResources().getString(R.string.failed_load_data),
						Toast.LENGTH_LONG).show();
				e.printStackTrace();
			} catch (IOException e) {
				Toast.makeText(context,
						getResources().getString(R.string.failed_load_data),
						Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}

			return json;
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			dialog.dismiss();
			try {
				if (result.getString("response").equalsIgnoreCase("success")) {
					if (result.has("error")) {
						Toast.makeText(MangaOverall.this,
								result.getString("error"), Toast.LENGTH_SHORT)
								.show();
						finish();
						return;
					} else {
						adapter = new MangaOverallAdapter(
								getSupportFragmentManager());

						pager = (ViewPager) findViewById(R.id.pager);
						pager.setAdapter(adapter);

						indicator = (TitlePageIndicator) findViewById(R.id.indicator);
						indicator.setViewPager(pager);

					}
				} else {
					Toast.makeText(MangaOverall.this, "Load Data Failed!",
							Toast.LENGTH_LONG).show();
					finish();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}
}
